package com.xyy.saas.payment.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xyy.saas.payment.cores.api.PaymentOrderApi;
import com.xyy.saas.payment.cores.bo.TransactionResultBO;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.param.TransactionParam;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.manage.cores.vo.ResultPageVo;
import com.xyy.saas.payment.pojo.bo.QueryFuminRecordBo;
import com.xyy.saas.payment.pojo.dto.PayTradeRecordDTO;
import com.xyy.saas.payment.pojo.vo.PayTradeRecordQueryParam;
import com.xyy.saas.payment.pojo.vo.QueryFuminRecordVo;
import com.xyy.saas.payment.service.excel.AnalysisFile;
import com.xyy.saas.payment.service.report.QueryFuminService;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 账单查询
 */
@RestController
@Slf4j
@RequestMapping("/queryTrade")
public class QueryTradeController {

    @Autowired
    private QueryFuminService queryFuminService;

    @Reference(version = "1.0.0")
    private PaymentOrderApi paymentOrderApi;

    /**
     * 查询交易记录
     */
    @GetMapping("/list")
    public ResultVO<PayTradeRecordDTO> tradeRecords(PayTradeRecordQueryParam param) {
        if (param == null || (StringUtils.isBlank(param.getPayNo()) && StringUtils.isBlank(param.getYbmOrderNo()))) {
            return ResultVO.createError(ResultCodeEnum.PARAM_ERROR);
        }
        // mock
        PayTradeRecordDTO payTradeRecordDTO = new PayTradeRecordDTO()
                .setTradeRecords(mockTradeRecords())
                .setTradeRecordExtends(mockTradeRecordExtends());
        return ResultVO.createSuccess(payTradeRecordDTO);
    }

    private List<PayTradeRecordDTO.TradeRecordExtend> mockTradeRecordExtends() {
        List<PayTradeRecordDTO.TradeRecordExtend> list = new ArrayList<>();
        PayTradeRecordDTO.TradeRecordExtend tradeRecordExtend = new PayTradeRecordDTO.TradeRecordExtend()
                .setBusinessOrderNo("1841706259384909880")
                .setSeparateNo("1843579516035809321")
                .setThirdTradeNo("2410089189368261")
                .setTradeType(2)
                .setTradeTime(new Date())
                .setTradeStatus(2)
                .setRemark1("会员间交易");
        list.add(tradeRecordExtend);
        return list;
    }

    private List<PayTradeRecordDTO.TradeRecord> mockTradeRecords() {
        List<PayTradeRecordDTO.TradeRecord> list = new ArrayList<>();
        PayTradeRecordDTO.TradeRecord tradeRecord = new PayTradeRecordDTO.TradeRecord()
                .setYbmOrderNo("YBM20241003124335132511")
                .setPayNo("1841701086327492698")
                .setMerchantId("1111354388")
                .setMerchantName("叶县刘建停内科诊所")
                .setOrgId("SH10068596")
                .setCompanyName("诚康濮阳旗舰店")
                .setActualPaymentAmount(new BigDecimal("99.99"))
                .setCreateTime(new Date())
                .setPayTime(new Date());
        list.add(tradeRecord);
        return list;
    }

    /** 查询富民账户明细 */
    @RequestMapping(value = "/fuminRecord", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResultPageVo queryFuminRecord(@RequestBody QueryFuminRecordVo queryFuminRecordVo) {
        log.info("QueryTradeController#queryFuminRecord QueryFuminRecordVo:{}", JSONUtils.toJSON(queryFuminRecordVo));
        try {
            // 时间格式化
            queryFuminRecordVo.setBeginTime(DateUtil.format(Long.valueOf(queryFuminRecordVo.getBeginTime()), DateUtil.DATEFORMATDAY));
            queryFuminRecordVo.setEndTime(DateUtil.format(Long.valueOf(queryFuminRecordVo.getEndTime()), DateUtil.DATEFORMATDAY));
            QueryFuminRecordBo queryFuminRecordBo = queryFuminService.queryFuminRecord(queryFuminRecordVo);
            return ResultPageVo.createSuccess(queryFuminRecordBo.getPageNo(), 20, queryFuminRecordBo.getTotalSize(), queryFuminRecordBo.getRecords());
        } catch (PaymentException e) {
            log.info("QueryTradeController#queryFuminRecord error queryVo:{} e:{}", JSONUtils.toJSON(queryFuminRecordVo), e.getErrCode()+e.getErrMsg());
            return ResultPageVo.createFail(e.getErrMsg());
        }
    }

    @Autowired
    private AnalysisFile AnalysisPayRecordExcelService;

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    public void queryDownload(@RequestParam(value = "file") MultipartFile file, HttpServletResponse response) {
        try {
            log.info("queryDownload start");
            AnalysisPayRecordExcelService.analysisExcel(file, response);
        } catch (Exception e) {
            log.info("QueryTradeController#queryDownload error. ", e);
        }
    }
    /** 查询富民账户明细 */
    @RequestMapping(value = "/queryTransactionInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResultVO<TransactionResultBO> queryTransactionInfo(@RequestBody TransactionParam param) {
        log.info("QueryTradeController#queryTransactionInfo TransactionParam:{}", JSONUtils.toJSON(param));
        return paymentOrderApi.queryTransactionInfo(param, "");
    }

}
